package com.burgeon.backend.Rest.coupon.util;
/**
 * 创建积分调整单
 */

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class CreateIntegral extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public CreateIntegral(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }
    /**
     * 发送并接收字符
     */
    public String getData(String phone,String integral) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(phone,integral), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String message = new JSONArray(str).getJSONObject(0).getString("message");
         if (!"0".equals(code)) {
            throw new Exception(message);
        }

        return new JSONArray(str).getJSONObject(0).getString("message").substring(0,15);
    }

    private MultiValueMap Data(String phone,String integral) {

        HashMap params = new HashMap();
        HashMap masterobj = new HashMap(); //头表记录对象
        masterobj.put("table", "WX_VIPINTEGRALADJ");
        masterobj.put("ID", "-1");
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateString = sdf.format(now);
        masterobj.put("BILLDATE", dateString);
        masterobj.put("ADJTYPE ", "4");

        masterobj.put("DESCRIPTION", "线上小程序商城积分调整");

        HashMap detailobjs = new HashMap(); //明细表记录对象
        ArrayList list = new ArrayList();
        list.add(4355);

        detailobjs.put("reftables", list);
//****************************************************************************
        ArrayList refobjs = new ArrayList();
        HashMap ref1 = new HashMap();
        ref1.put("table", "WX_VIPINTEGRALADJITEM");//积分调整单单明细
        List addList1 = new ArrayList();
        HashMap map1 = new HashMap();//addlist内容
        map1.put("id", "-1");
        map1.put("C_VIP_ID__CARDNO", phone);

        map1.put("INTEGRALADJ", -Integer.parseInt(integral));

        addList1.add(map1);

        ref1.put("addList", String.valueOf(addList1));

        refobjs.add(ref1);

        detailobjs.put("refobjs", refobjs);

        params.put("masterobj", masterobj);
        params.put("detailobjs", detailobjs);
        params.put("submit", "true");//是否提交
        return DATA("ProcessOrder", params, account, password);
    }

    public static void main(String[] args) {
        CreateIntegral g = new CreateIntegral("39.99.239.133:55321", "nea@burgeon.com.cn", "burgeon55321");
        try {
           String a = g.getData("17732141036","-10");
            System.err.println(a);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
